// pages/addaddress/addaddress.js
const util = require("../../utils/util")
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:{},
    array: ['我的地址', '客户地址'],
    type:['my_address','client_address'],
    addrTypeIndex: 0,
    province: 18,
    district: 0,
    city: 0,
    name:"",
    tel:"",
    detail:"",
    addr:{}
  },
  bindPickerChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      addrTypeIndex: e.detail.value
    })
  },
  bindRegionChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      [e.currentTarget.dataset.id]: e.detail.value
    })
  },
  bindInput(e){
    this.setData({
      [e.currentTarget.dataset.id]:e.detail.value
    })
  },
  bindSave(){
    let that = this
    wx.showLoading({
      title: '加载中'
    })
    util.post({
      url:"https://groomtea.icebartech.com/api/miniApp/dealer/saveShippingAddress",
      success:(res)=>{
        console.log(res) 
        app.titleToast(res.msg)
        if(res.status=='200'){
          
          wx.navigateBack({
            
          })
          wx.showToast({
            title: '保存成功',
          })
        }
        
      },
      data:{
        "id":that.data.addr.id,
        "addressType": that.data.type[that.data.addrTypeIndex],
        "cityCode": that.data.list[that.data.province].childs[that.data.city] ? that.data.list[that.data.province].childs[that.data.city].key : 0,
        "consignee": that.data.addr.consignee,
        "detailAddress": that.data.addr.detailAddress,
        "districtCode": that.data.list[that.data.province].childs[that.data.city].childs[that.data.district] ? that.data.list[that.data.province].childs[that.data.city].childs[that.data.district].key : 0,
        "mobile": that.data.addr.mobile,
        "provinceCode": that.data.list[that.data.province].key
      },
      fail:(error)=>{
        console.log(error)
        app.titleToast(error.msg)
      },
      complete:function(){
          
      }
    })
  } ,
  onLoad:function(options){
    let that = this;
    let addrType = that.data.type;
    let typeIndex = 0;
    let addr = options.addr || {};
    let provin, ct,distri;
    wx.request({
      url: "https://groomtea.icebartech.com/api/base/sys/mini/loadDictionary?codeType=province",
      header: { "Accept": "application/json;charset=UTF-8" },
      method: 'post',
      success(res) {
        let list = res.data.data.bussData;
        console.log(addr)
        if (typeof (addr) === 'string') {
          addr = JSON.parse(addr);
          for (let j = 0; j < addrType.length; j++) {
            if (addr.addressType == addrType[j]) {
              typeIndex = j;
            }
          }
          list.some ((province,index)=>{
            console.log(province)
            if (addr.provinceCode && province.key == addr.provinceCode){
              provin = index;
              console.log(provin)
              province.childs.some ((city,index)=>{
                console.log(city)
                if (addr.cityCode && city.key == addr.cityCode){
                  ct = index;
                  city.childs.some ((district,index)=>{
                    if (addr.districtCode && district.key == addr.districtCode){
                      distri = index;
                      return true;
                    }
                  });
                  return true;
                }
              })
              return true;
            };
          })
          that.setData({
            province: provin,
            city: ct,
            district: distri
          })
        }
        that.setData({
          addrTypeIndex: typeIndex,
          addr: addr,
          list:res.data.data.bussData,
        })
      }
    })
  }
})